/**
 * 
 */
package com.estock.platform.http;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.estock.common.model.security.RealTimeTrans;
import com.estock.common.model.security.Stock;
import com.estock.common.model.security.enums.StockType;
import com.estock.common.util.CommonUtil;
import com.estock.platform.http.client.GetClientProxy;
import com.estock.platform.http.client.URLConnectionClient;
import com.estock.platform.http.consts.RequestConsts;
import com.estock.platform.http.request.StockClientRequest;
import com.estock.platform.http.response.BaseResponse;
import com.estock.platform.http.response.SinaRealTimeStockBulkResponse;
import com.estock.platform.http.response.SinaStockBulkResponse;
import com.estock.platform.http.utils.RequestUtil;

/**
 * @author richard
 *
 */
public class SinaStockBulkConn extends AbstractConnection {
	
	private static Logger logger = Logger.getLogger(SinaStockBulkConn.class);
	
	public BaseResponse request(List<Stock> stocks, boolean isRealTimeTask) throws Exception {
		try {
			if (isRealTimeTask) {
				SinaRealTimeStockBulkResponse response = 
						getResponse(SinaRealTimeStockBulkResponse.class, stocks);
				if (CommonUtil.isUnitTest) {
					List<RealTimeTrans> realTimeTrans = response.getRealTimeTrans();
			    	if (realTimeTrans != null) {
			    		logger.info("get sinajs realtime transactions with size: " + realTimeTrans.size());
			    	}
				}
		    	
		    	return response;
			}
			
			SinaStockBulkResponse response = 
					getResponse(SinaStockBulkResponse.class, stocks);
			if (CommonUtil.isUnitTest) {
				List<Stock> responsedStocks = response.getStocks();
		    	if (stocks != null) {
		    		logger.info("get sinajs bulk stocks with size: " + responsedStocks.size());
		    	}
			}
			return response;
		} catch (Exception e) {
			throw new RuntimeException("can not get valid stocks information.", e);
		}
        
	}
	
	@Override
	protected GetClientProxy createStockClientProxy() {
		return new GetClientProxy(new URLConnectionClient() {
			@Override
			protected String saveStreamAsString(InputStream inputStream) 
					throws IOException {
				return RequestUtil.saveStreamAsGBK(inputStream);
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	protected StockClientRequest buildStockClientRequest(Object... params) throws Exception {
		return (StockClientRequest) StockClientRequest
				.getSinaJSRequestBuilder(RequestConsts.URI_SINA_STOCK_JS)
				.appendBulkStocks((List<Stock>) params[0])
                .buildQueryMessage();
	}
	
	
	/**
	 * Test Method
	 * 
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		List<Stock> stocks = new ArrayList<Stock>();
		stocks.add(new Stock("000413", "", StockType.SZ.getType()));
		new SinaStockBulkConn().request(stocks, false);
	}

}
